//
//  SinaVisitView.swift
//  Sina
//
//  Created by 星仔 on 2017/2/28.
//  Copyright © 2017年 xingZai. All rights reserved.
//

import UIKit

class SinaVisitView: UIView {
 
    lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var scrollImageView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    lazy var shadeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy var desLabel:UILabel = UILabel()
    lazy var reginButton:UIButton = UIButton()
    lazy var loginButton:UIButton = UIButton()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
     //重写父类的方法
    //外部传入字典访问信息
    var visitorInfo:[String:String]?{
        didSet{
            let message = visitorInfo?["message"]
            let imageName = visitorInfo?["imageName"]
            
            if imageName == "" || imageName == nil {//首页
                
                //设置动画
                let anim = CABasicAnimation(keyPath: "transform.rotation")//旋转动画
                anim.toValue = 2*M_PI //旋转值 
                anim.repeatCount = MAXFLOAT //不停的旋转
                anim.duration = 15
                anim.isRemovedOnCompletion = false //完成后不删除，如果这个视图删除，动画停止，这里设置可以不停止
                
                scrollImageView.layer.add(anim, forKey: nil) //添加到layer层上
                
                return
            }
            
            backImageView.image = UIImage(named: "")
            desLabel.text = message
            scrollImageView.image = UIImage(named: imageName!)
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
extension SinaVisitView{
    
    
    
    func setupUI(){
        
        backgroundColor = UIColor.init(colorLiteralRed: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
    
        addSubview(backImageView)
        addSubview(shadeImageView)
        addSubview(scrollImageView)
        addSubview(desLabel)
        addSubview(reginButton)
        addSubview(loginButton)
        
        
        desLabel.text = "关注一些人，回这里看看有什么惊喜关注一些,人回这里看看有什么惊喜关注一些人"
        desLabel.font = UIFont.systemFont(ofSize: 14)
        desLabel.numberOfLines = 0
        desLabel.isUserInteractionEnabled = true
        desLabel.lineBreakMode = .byCharWrapping
        desLabel.textAlignment = .center
        reginButton.setTitle(NSLocalizedString("注册", comment: "regin"), for: .normal)
        reginButton.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        reginButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        loginButton.setTitle(NSLocalizedString("登录", comment: "login"), for: .normal)
        loginButton.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        backImageView.contentMode = .scaleAspectFit
        scrollImageView.contentMode = .scaleAspectFit
        
        //代码约束时这个要设置成false
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置x,y，因为是图片，可以只设置x,y
        addConstraint(NSLayoutConstraint(item: backImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: backImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        //遮罩
        addConstraint(NSLayoutConstraint(item: shadeImageView,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: backImageView,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: -150))
        
        addConstraint(NSLayoutConstraint(item: shadeImageView,
                                         attribute: .bottom,
                                         relatedBy: .equal,
                                         toItem: reginButton,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImageView,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //set scrollImageView
        addConstraint(NSLayoutConstraint(item: scrollImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: backImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: scrollImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: backImageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //label
        addConstraint(NSLayoutConstraint(item: desLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: scrollImageView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 30))
        addConstraint(NSLayoutConstraint(item: desLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        //自身的高度不用相对于某个控件这时用nil
        addConstraint(NSLayoutConstraint(item: desLabel,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .height,
                                         multiplier: 1,
                                         constant: 50))
        addConstraint(NSLayoutConstraint(item: desLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 0))
        
        
        //regin button
        addConstraint(NSLayoutConstraint(item: reginButton,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 0.5,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: reginButton,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: desLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 30))
        addConstraint(NSLayoutConstraint(item: reginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 100))
        //login button
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: reginButton,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: UIScreen.main.bounds.width/2))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: desLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 100))
    }
    
}

